#include<iostream>
const int N = 1e6 + 10;
int ne[N], e[N], mp[N],n,h,id;
using namespace std;
int main()
{
	cin >> n;
	id++;
	e[id] = 1;
	mp[1] = id;
	while (n--)
	{
		int a, x, y;
		cin >> a >> x;
		int p = mp[x];
		if (a == 1)
		{
			cin >> y;
			id++;
			e[id] = y;
			ne[id] = ne[p];
			ne[p] = id;
			mp[y] = id;
		}
		else if (a == 2)
		{
			cout <<  e[ne[p]] << endl;
		}
		else if (a == 3)
		{
			mp[e[ne[p]]] = 0;
			ne[p] = ne[ne[p]];
		}
	}
	return 0;
}